#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct data { int w, s; };
vector<data> da;

struct cmp { bool operator()(data a, data b) { return a.w < b.w; } } cmp;
int n;

bool judge(int x)
{
	vector<data> jud;
	jud.resize(n);
	for (int i = 0; i < n; i++) {
		jud[i].w = (x + da[i].s + da[i].w);
		jud[i].s = da[i].w;
	}
	sort(jud.begin(), jud.end(), cmp);

	int sum = 0;
	for (int i = 0; i < n; i++) {
		sum += jud[i].s;
		if (sum > jud[i].w) return false;
	}
	return true;
}

int main()
{
	ios::sync_with_stdio(false);
	cin >> n;
	da.resize(n);
	int max_s = 0, min_s = 1<<30, sum = 0;
	for (int i = 0; i < n; i++) {
		cin >> da[i].w >> da[i].s;
		max_s = max(max_s, da[i].s);
		min_s = min(min_s, da[i].s);
		sum += da[i].w;
	}
	sort(da.begin(), da.end(), cmp);
	int l = sum-da[n-1].w - max_s, r = sum-da[0].w - min_s;
	while (l+1 < r) {
		int mid = (l+r)/2;
		if (judge(mid)) r = mid;
		else            l = mid+1;
	}
	if (judge(l)) r = l;
	cout << r << "\n";
}

